from functools import lru_cache

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap

from settings import IMAGE_DIR

# 文字左对齐
FLAG_ALIGN_LEFT = Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter
# 文字居中
FLAG_ALIGN_CENTER = Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignHCenter

# 环境描述
ENV_DESC = ('死寂', '恶劣', '一般', '优秀', '仙境')


@lru_cache
def get_pixmap(path: str):
    return QPixmap(path)


def get_resource_icon(name):
    icon_path = IMAGE_DIR / 'icon' / f'{name}.png'
    return get_pixmap(str(icon_path))


def format_number(number, monthly=False):
    abs_number = abs(number)
    pre = "-" if number < 0 else ("+" if monthly else "")

    if abs_number < 1000:
        result = abs_number
    elif 1000 <= abs_number < 1000000:
        result = str(round(abs_number / 1000, 2)) + "K"
    elif 1000000 <= abs_number < 1000000000:
        result = str(round(abs_number / 1000000, 2)) + "M"
    elif 1000000000 <= abs_number < 1000000000000:
        result = str(round(abs_number / 1000000000, 2)) + "G"
    else:
        result = "MAX"

    return f"{pre}{result}"
